package com.iotechn.unimall.admin.api.seckill;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.biz.service.goods.GoodsBizService;
import com.iotechn.unimall.biz.service.seckill.SeckillSkuBizService;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.component.CacheComponent;
import com.iotechn.unimall.data.domain.SeckillSkuDO;
import com.iotechn.unimall.data.domain.SeckillSpuDO;
import com.iotechn.unimall.data.domain.SkuDO;
import com.iotechn.unimall.data.domain.SpuDO;
import com.iotechn.unimall.data.dto.seckill.SeckillSkuDTO;
import com.iotechn.unimall.data.dto.seckill.SeckillSpuDTO;
import com.iotechn.unimall.data.enums.OrderBuyType;
import com.iotechn.unimall.data.enums.SeckillStatusType;
import com.iotechn.unimall.data.enums.SpuStatusType;
import com.iotechn.unimall.data.mapper.SeckillSkuMapper;
import com.iotechn.unimall.data.mapper.SeckillSpuMapper;
import com.iotechn.unimall.data.mapper.SkuMapper;
import com.iotechn.unimall.data.mapper.SpuMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class AdminSeckillServiceImpl implements AdminSeckillService {



    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private CacheComponent cacheComponent;

    private static final String SKILL_SHOP_CACHE = "CA_SKILL_SHOP";

    @Autowired
    private SeckillSkuMapper  seckillSkuMapper;

    @Autowired
    private SeckillSpuMapper seckillSpuMapper;

    @Autowired
    AdminSeckillService adminSeckillService;

    @Autowired
    SeckillSkuBizService seckillSkuBizService;

    @Autowired
    GoodsBizService goodsBizService;

     @Override
     public Page<SeckillSpuDTO> list(String spuName,Integer status,Integer page, Integer limit, Long adminId,Long id) throws ServiceException {
             Wrapper<SeckillSpuDO> wrapper = new EntityWrapper<SeckillSpuDO>();
         if (status != null) {
             wrapper.eq("status", status);
         }
         if (spuName != null) {
             wrapper.like("spu_title", spuName);
         }
         if(id != null){
             wrapper.eq("id", id);
         }
            wrapper.ne("status",SeckillStatusType.SECKILLDISMOUNT.getCode());
            wrapper.orderBy("gmt_start", false);
            Integer count = seckillSpuMapper.selectCount(wrapper);
            List<SeckillSpuDO> SeckillSpuDOS = seckillSpuMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
            if(status ==null && spuName == null&& id == null){
                Wrapper<SeckillSpuDO> wrapperDown = new EntityWrapper<SeckillSpuDO>();
                wrapperDown.eq("status", SeckillStatusType.SECKILLDISMOUNT.getCode());
                Integer  countDown = seckillSpuMapper.selectCount(wrapperDown);
                if(SeckillSpuDOS.size() < limit || SeckillSpuDOS.size() ==0){
                    List<SeckillSpuDO> SeckillSpuDOSDown=new ArrayList<>();
                    if(countDown > limit){
                         SeckillSpuDOSDown = seckillSpuMapper.selectPage(new RowBounds(0, limit), wrapperDown);
                    }
                    if(countDown <= limit){
                        SeckillSpuDOSDown = seckillSpuMapper.selectPage(new RowBounds(0, countDown), wrapperDown);
                    }

                    SeckillSpuDOS.addAll(SeckillSpuDOSDown);
                }
                count = count + countDown;
            }
//            //查询 下架商品
            if(status != null && SeckillStatusType.SECKILLDISMOUNT.getCode()==status){
                Wrapper<SeckillSpuDO> wrapperDown = new EntityWrapper<SeckillSpuDO>();
                count=0;
                SeckillSpuDOS=new ArrayList<>();
                wrapperDown.eq("status", SeckillStatusType.SECKILLDISMOUNT.getCode());
                Integer countDown = seckillSpuMapper.selectCount(wrapper);
                List<SeckillSpuDO> SeckillSpuDOSDown = seckillSpuMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapperDown);
                count = count + countDown;
                SeckillSpuDOS.addAll(SeckillSpuDOSDown);
            }


            List<SeckillSpuDTO> seckillSpuDTOList = new LinkedList<>();

         for (SeckillSpuDO seckillSpuDO : SeckillSpuDOS) {
             SeckillSpuDTO seckillSpuDTO = new SeckillSpuDTO();
             SpuDO spuDO = spuMapper.selectById(seckillSpuDO.getSpuId());
             if(spuDO==null)continue;
             BeanUtils.copyProperties(spuDO, seckillSpuDTO);
             BeanUtils.copyProperties(seckillSpuDO, seckillSpuDTO);
             /**
              * 添加seckillSkuDTOList
              */
             List<SkuDO> skuDOList = skuMapper.selectList((new EntityWrapper<SkuDO>().eq("spu_id", spuDO.getId())));
             List<SeckillSkuDO> seckillSkuDOList = seckillSkuMapper.selectList((new EntityWrapper<SeckillSkuDO>()).eq("seckill_spu_id", seckillSpuDO.getId()));
             List<SeckillSkuDTO> seckillSkuDTOList = seckillSkuMapper.selectSeckillSkuList(seckillSpuDO.getId());
//             List<SeckillSkuDTO> seckillSkuDTOList = seckillSkuDOList.stream().map(s -> {
//                 SeckillSkuDTO seckillSkuDTO = new SeckillSkuDTO();
//                 BeanUtils.copyProperties(s, seckillSkuDTO);
//                 return seckillSkuDTO;
//             }).collect(Collectors.toList());

             for (SkuDO skuDO : skuDOList) {
                 for (SeckillSkuDTO seckillSkuDTO : seckillSkuDTOList) {
                     if (seckillSkuDTO.getSkuId().equals(skuDO.getId())) {
                         BeanUtils.copyProperties(skuDO, seckillSkuDTO);
                         break;
                     }
                 }
             }
             seckillSpuDTO.setSeckillSkuDTOList(seckillSkuDTOList);
             seckillSpuDTOList.add(seckillSpuDTO);
         }
         return new Page<SeckillSpuDTO>(seckillSpuDTOList, page, limit, count);

        }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(Long adminId, Long spuId, Long gmtStart, Long gmtEnd,List seckillSkuList,Float limitCount,Integer seckillPrice, Boolean orderBuyType) throws ServiceException {
         // 1.转化为对应的SeckillSkuDo类的链表
        List<SeckillSkuDO> seckillSkuDOList = (List<SeckillSkuDO>) seckillSkuList.stream().map(t -> {
            SeckillSkuDO seckillSkuDO = JSONObject.toJavaObject((JSON) t, SeckillSkuDO.class);
            return seckillSkuDO;
        }).sorted(Comparator.comparingInt(o -> ((SeckillSkuDO) o).getSkuSeckillPrice())).collect(Collectors.toList());

        if (gmtStart.compareTo(gmtEnd) >= 0) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_START_MUST_LESS_THAN_END);
        }

        // 2.检验数据库中是否存在spuId对应的sku
        SpuDO spuDO = spuMapper.selectById(spuId);
        //判断商品是否下架
        if(SpuStatusType.STOCK.getCode() == spuDO.getStatus()){
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_STATUS_STOCK);
        }


        List<SkuDO> skuDOList = skuMapper.selectList((new EntityWrapper<SkuDO>().eq("spu_id", spuId)));
        if (spuDO == null || CollectionUtils.isEmpty(skuDOList)) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_NO_EXITS_OR_ONLY_SPU);
        }
        Date timeStart = new Date(gmtStart);
        Date timeEnd = new Date(gmtEnd);
        Date now = new Date();
        SeckillSpuDO seckillSpuDO = new SeckillSpuDO();

        seckillSpuDO.setGmtStart(timeStart);
        seckillSpuDO.setGmtEnd(timeEnd);
        seckillSpuDO.setSpuId(spuId);
        seckillSpuDO.setGmtCreate(now);
        seckillSpuDO.setGmtUpdate(now);
        seckillSpuDO.setSpuSeckillPrice(seckillPrice);//spu 显示价格
        seckillSpuDO.setLimitCount(limitCount);//秒杀限制
        seckillSpuDO.setSpuTitle(spuDO.getTitle());
        seckillSpuDO.setSeckillSpuSales(0f);
        //计算总库存
        Float spuStockCount=0.000f;
        for ( SeckillSkuDO seckillSkuDO: seckillSkuDOList
             ) {
            spuStockCount=spuStockCount+seckillSkuDO.getStockCount();
        }
        seckillSpuDO.setStockCount(spuStockCount);
        //写入 购买类型 (true  == 0 == 自提)
        seckillSpuDO.setBuyType(OrderBuyType.DISPATCH.getCode());
        if(orderBuyType){
            seckillSpuDO.setBuyType(OrderBuyType.SELFEXTRACTING.getCode());
        }
        // 3.设置是否在活动时间的状态
        setStatus(seckillSpuDO);
        if (seckillSpuMapper.insert(seckillSpuDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_ADD_SQL_QUERY_ERROR);
        }

        if (skuDOList.size() != seckillSkuList.size()) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_NUMBER_ERROR);
        }
        // 4.插入seckillSkuList
        this.insertSeckillSkuList(seckillSkuDOList, skuDOList, seckillSpuDO, now);
         cacheComponent.delPrefixKey(SKILL_SHOP_CACHE);
        return "ok";
    }

    /**
     * 新增秒杀
     * @param seckillSkuList
     * @param skuDOList
     * @param now
     * @throws ServiceException
     */
    public void insertSeckillSkuList(List<SeckillSkuDO> seckillSkuList, List<SkuDO> skuDOList, SeckillSpuDO seckillSpu, Date now) throws ServiceException {
        for (SeckillSkuDO seckillSkuDO : seckillSkuList) {
            seckillSkuDO.setId(null);
            boolean judge = false;
            for (SkuDO sku : skuDOList) {
                if (sku.getBarCode().equals(seckillSkuDO.getBarCode())) {
                    judge = true;
                    break;
                }
            }

            if (!judge) {
                throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_ID_ERROR);
            }

            if (seckillSkuDO.getSkuSeckillPrice() == null || seckillSkuDO.getSkuSeckillPrice().equals(0)) {
                throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_PRICE_ERROR);
            }
            //查询大库得商品sku  库存
            SkuDO skuDo = skuMapper.selectById(seckillSkuDO.getSkuId());
            //秒杀商品 在大库中查不到
            if(skuDo ==null){
                throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_ADD_SQL_QUERY_ERROR);
            }
            if(skuDo.getStock()< seckillSkuDO.getStockCount()){
                // 秒杀库存大于 大库库存
                throw new AdminServiceException(ExceptionDefinition.SECKILL_STOCKCOUNT_ERROR);
            }
            // 秒杀库商品从  大库获取库存量
            skuDo.setStock(skuDo.getStock()-seckillSkuDO.getStockCount().intValue());
            Integer integer = skuMapper.updateById(skuDo);

            //大库减库存失败
            if(integer < 1 ){
                seckillSkuDO.setStockCount(0.000f);//扣减大库库存失败  秒杀量为0
            }
            seckillSkuDO.setSeckillSpuId(seckillSpu.getId());
            seckillSkuDO.setGmtCreate(now);
            seckillSkuDO.setGmtUpdate(now);
            seckillSkuDO.setSalesVolume(0.000f);
            seckillSkuDO.setFreeStockCount(0.000f);//冻结库存为默认值
            seckillSkuDO.setSpuId(skuDo.getSpuId());
            seckillSkuDO.setBarCode(skuDo.getBarCode());
            seckillSkuDO.setSkuTitle(skuDo.getTitle());
            seckillSkuDO.setSkuLimitCount(seckillSpu.getLimitCount());
            Integer insert = seckillSkuMapper.insert(seckillSkuDO);

            if (insert <= 0) {
                throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_ADD_SQL_QUERY_ERROR);
            }
        }
    }

    /**
     * 根据时间修改状态
     * @param seckillSpuDO
     */
    private void setStatus(SeckillSpuDO seckillSpuDO) {
        Date gmtStart = seckillSpuDO.getGmtStart();
        Date gmtEnd = seckillSpuDO.getGmtEnd();
        Date currentTime=new Date();
        //  当前时间在活动期间  恢复活动
        if(gmtStart.getTime() < currentTime.getTime() && gmtEnd.getTime() > currentTime.getTime()){
            seckillSpuDO.setStatus(SeckillStatusType.SECKILLSUSPEND.getCode());//StatusType.ACTIVE.getCode()
        }
        //  当前时间未到 活动时间  未开启
        if(gmtStart.getTime() > currentTime.getTime() ){
            seckillSpuDO.setStatus(SeckillStatusType.BEFORESECKILL.getCode());
        }
        // 当前时间  已过活动时间
        if(gmtEnd.getTime() < currentTime.getTime()){
            seckillSpuDO.setStatus(SeckillStatusType.SECKILLOVER.getCode());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {

        SeckillSpuDO seckillSpuDO = seckillSpuMapper.selectById(id);
        if (seckillSpuDO == null) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_NO_EXITS_OR_ONLY_SPU);
        }

        if (seckillSpuDO.getStatus() == 1) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_ALREAD_ATCIVE);
        }

        // 根据spu id 查询 skuList  循环将库存归还大库
        List<SeckillSkuDO> seckillSkuList = seckillSkuMapper.selectList((new EntityWrapper<SeckillSkuDO>().eq("seckill_spu_id", id)));
        for (SeckillSkuDO  SeckillSkuDO : seckillSkuList
             ) {
            if(SeckillSkuDO==null)continue;
            SkuDO skuDO = skuMapper.selectById(SeckillSkuDO.getSkuId());
            if(skuDO==null) continue;
            skuDO.setStock(skuDO.getStock()+SeckillSkuDO.getStockCount().intValue());
            seckillSkuMapper.updateById(SeckillSkuDO);
            Integer integer = skuMapper.updateById(skuDO);
            SeckillSkuDO.setStockCount(0f);
            seckillSkuMapper.updateById(SeckillSkuDO);
            if(integer< 1){
                throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_DELETE_AND_UDPATE_SKU);
        }
        }

        seckillSpuDO.setStatus(SeckillStatusType.SECKILLDISMOUNT.getCode());
        if (seckillSpuMapper.updateById(seckillSpuDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_DELETE_SQL_QUERY_ERROR);
        }

//        if (seckillSkuMapper.delete((new EntityWrapper<SeckillSkuDO>().eq("seckill_spu_id", id))) <= 0) {
//            throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_DELETE_SQL_QUERY_ERROR);
//        }
       cacheComponent.delPrefixKey(SKILL_SHOP_CACHE);
        return "ok";
    }

    /**
     * 秒杀活动下架
     * @param id
     * @param adminId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String suspend(Long id, Long adminId) throws ServiceException {
        SeckillSpuDO seckillSpuDO = seckillSpuMapper.selectById(id);
        if (seckillSpuDO == null) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_NO_EXITS_OR_ONLY_SPU);
        }
        try {
            seckillSkuBizService.deleteSeckillSku(id);
        } catch (Exception e) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_START_ERROR);
        }
        //状态修改为暂停
        seckillSpuDO.setStatus(2);
        if(seckillSpuMapper.updateById(seckillSpuDO) < 1){
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_DELETE_SQL_QUERY_ERROR);
        }

        return "ok";
    }

    /**
     *  启动暂停的 秒杀
     * @param id
     * @param adminId
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String start(Long id, Long adminId) throws ServiceException {
        SeckillSpuDO seckillSpuDO = seckillSpuMapper.selectById(id);
        if (seckillSpuDO == null) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_NO_EXITS_OR_ONLY_SPU);
        }
        //进行时间判断
        this.setStatus(seckillSpuDO);
        //将暂停状态的开启
        if(seckillSpuDO.getStatus()==SeckillStatusType.SECKILLSUSPEND.getCode()){
            seckillSpuDO.setStatus(SeckillStatusType.SECKILLING.getCode());
        }
        if(seckillSpuMapper.updateById(seckillSpuDO) < 1){
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_UPDATE_SQL_QUERY_ERROR);
        }
            try {
                seckillSkuBizService.makeredisCaCheSeckillSkuById(id);
            } catch (Exception e) {
                throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_START_ERROR);
            }
        return "ok";
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public SeckillSpuDTO  update(Long adminId, Long id, Long spuId, Long gmtStart, Long gmtEnd, List seckillSkuList,Float limitCount,Integer seckillPrice,Boolean orderBuyType) throws ServiceException {
         // 1.转化为对应的SeckillSkuDo类的链表
        List<SeckillSkuDO> seckillSkuDOList = (List<SeckillSkuDO>) seckillSkuList.stream().map(t -> {
            SeckillSkuDO seckillSkuDO = JSONObject.toJavaObject((JSON) t, SeckillSkuDO.class);
            return seckillSkuDO;
        }).sorted(Comparator.comparingInt(o -> ((SeckillSkuDO) o).getSkuSeckillPrice())).collect(Collectors.toList());

        if (gmtStart.compareTo(gmtEnd) >= 0) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_START_MUST_LESS_THAN_END);
        }

        SpuDO spuDO = spuMapper.selectById(spuId);
        //判断商品是否下架
        if(SpuStatusType.STOCK.getCode() == spuDO.getStatus()){
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_STATUS_STOCK);
        }
        List<SkuDO> skuDOList = skuMapper.selectList((new EntityWrapper<SkuDO>().eq("spu_id", spuId)));
        if (spuDO == null || CollectionUtils.isEmpty(skuDOList)) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_NO_EXITS_OR_ONLY_SPU);
        }

        SeckillSpuDO seckillSpuDO = seckillSpuMapper.selectById(id);
        if (seckillSpuDO == null) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_NO_EXITS);
        }

        if (seckillSpuDO.getStatus() == 1) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_ALREAD_ATCIVE);
        }


        Date timeStart = new Date(gmtStart);
        Date timeEnd = new Date(gmtEnd);
        Date now = new Date();

        seckillSpuDO.setGmtStart(timeStart);
        seckillSpuDO.setGmtEnd(timeEnd);
        seckillSpuDO.setSpuId(spuId);
        seckillSpuDO.setGmtUpdate(now);
        seckillSpuDO.setSpuSeckillPrice(seckillPrice);//spu 显示价格
        seckillSpuDO.setLimitCount(limitCount);//秒杀限制
        //计算总库存
        Float spuStockCount=0.000f;
        for ( SeckillSkuDO seckillSkuDO: seckillSkuDOList
        ) {
            spuStockCount=spuStockCount+seckillSkuDO.getStockCount();
        }
        seckillSpuDO.setStockCount(spuStockCount);
        seckillSpuDO.setBuyType(OrderBuyType.DISPATCH.getCode());
        if(orderBuyType){
            seckillSpuDO.setBuyType(OrderBuyType.SELFEXTRACTING.getCode());
        }
        this.setStatus(seckillSpuDO);
        if (seckillSpuMapper .updateById(seckillSpuDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SPU_UPDATE_SQL_QUERY_ERROR);
        }

        if (skuDOList.size() != seckillSkuDOList.size()) {
            throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_NUMBER_ERROR);
        }
        // 4.插入seckillSkuList
        this.updateSeckillSkuList(id,seckillSkuDOList, skuDOList, seckillSpuDO, now);
         cacheComponent.delPrefixKey(SKILL_SHOP_CACHE);
        return new SeckillSpuDTO();
    }

    /**
     * 修改秒杀
     * @param seckillSkuList
     * @param skuDOList
     * @param now
     * @throws ServiceException
     */
    private void updateSeckillSkuList(Long id,List<SeckillSkuDO> seckillSkuList, List<SkuDO> skuDOList, SeckillSpuDO seckillSpuDO, Date now) throws ServiceException {
        for (SeckillSkuDO seckillSkuDO : seckillSkuList) {
            boolean judge = false;
            for (SkuDO sku : skuDOList) {
                if (sku.getId().equals(seckillSkuDO.getSkuId())) {
                    judge = true;
                    break;
                }
            }

            if (!judge) {
                throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_ID_ERROR);
            }

            if (seckillSkuDO.getSkuSeckillPrice() == null || seckillSkuDO.getSkuSeckillPrice().equals(0)) {
                throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_PRICE_ERROR);
            }
            //查询修改前  秒杀量
            SeckillSkuDO param=new SeckillSkuDO();
            param.setSeckillSpuId(id);
            param.setSkuId(seckillSkuDO.getSkuId());
            SeckillSkuDO tmpSeckillSkuDO=seckillSkuMapper.selectById(seckillSkuDO.getId());
            if(tmpSeckillSkuDO == null ){
                throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_UPDATE_NOT_FIND);
            }
            //原秒杀库存
            Float stockCountOld =tmpSeckillSkuDO.getStockCount();
            // 原 - 新 = 差  (原 > 新  差 > 0  大库+ 差    原 <  新  差 <  0 大库+ 差)
            Float stockCountTmp=stockCountOld-seckillSkuDO.getStockCount();
            //查询大库得商品sku  库存
            SkuDO skuDo = skuMapper.selectById(seckillSkuDO.getSkuId());
            if(stockCountTmp.floatValue() < 0f){
                if(skuDo.getStock() + stockCountTmp < 0){
                    // 秒杀库存大于 大库库存
                    throw new AdminServiceException(ExceptionDefinition.SECKILL_STOCKCOUNT_ERROR);
                }
            }

            // 秒杀库商品从  大库获取库存量
            skuDo.setStock(skuDo.getStock()+stockCountTmp.intValue());
            Integer integer = skuMapper.updateById(skuDo);
            //大库减库存失败
            if(integer < 1 ){
                seckillSkuDO.setStockCount(0.00f);//扣减大库库存失败  秒杀量为0
            }
            seckillSkuDO.setSeckillSpuId(seckillSpuDO.getId());
            seckillSkuDO.setGmtUpdate(now);
            seckillSkuDO.setFreeStockCount(0.00f);//冻结库存为默认值
            seckillSkuDO.setSpuId(skuDo.getSpuId());
            seckillSkuDO.setBarCode(skuDo.getBarCode());
            seckillSkuDO.setId(tmpSeckillSkuDO.getId());
            seckillSkuDO.setSkuLimitCount(seckillSpuDO.getLimitCount());
            if (seckillSkuMapper.updateById(seckillSkuDO) <= 0) {
                throw new AdminServiceException(ExceptionDefinition.SECKILL_SKU_UPDATE_SQL_QUERY_ERROR);
            }
        }
    }

}